package com.example.lenovo.weiyi;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.com.ruanmeng.demon.CreateKFPDM;
import com.com.ruanmeng.demon.PerMessM;
import com.com.ruanmeng.utils.CommonUtil;
import com.com.ruanmeng.utils.Params;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

/**
 * 康复评定
 */
public class KangFuPDActivity extends BaseActivity {

    @BindView(R.id.tv_kfpd_name)
    TextView tvKfpdName;
    @BindView(R.id.tv_kfpd_years)
    EditText tvKfpdYears;
    @BindView(R.id.tv_kfpd_sg)
    EditText tvKfpdSg;
    @BindView(R.id.tv_kfpd_weight)
    EditText tvKfpdWeight;
    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kang_fu_pd);
        ButterKnife.bind(this);
        ChangLayTitle("健康档案");
        AddActivity(this);
        LayBack();
        init();
    }

    public void init() {
        Params.assessStatus = 0;
        Params.cureTarget = "";//新建的话吧已保存的目标清空
        if (!TextUtils.isEmpty(getIntent().getStringExtra("name"))) {
            tvKfpdName.setText(getIntent().getStringExtra("name"));
        } else {
            tvKfpdName.setText(PreferencesUtils.getString(KangFuPDActivity.this, "user_name"));
        }
        if (!TextUtils.isEmpty(PreferencesUtils.getString(KangFuPDActivity.this, "age")) && !("0").equals(PreferencesUtils.getString(KangFuPDActivity.this, "age"))) {
            tvKfpdYears.setText(PreferencesUtils.getString(KangFuPDActivity.this, "age"));
        } else {
            getPerData();
        }
        if (!TextUtils.isEmpty(PreferencesUtils.getString(KangFuPDActivity.this, "stature"))) {
            tvKfpdSg.setText(PreferencesUtils.getString(KangFuPDActivity.this, "stature"));
        }
        if (!TextUtils.isEmpty(PreferencesUtils.getString(KangFuPDActivity.this, "weight"))) {
            tvKfpdWeight.setText(PreferencesUtils.getString(KangFuPDActivity.this, "weight"));
        }
//        tvKfpdWeight.setText(getIntent().getStringExtra("tz"));
//        tvKfpdSg.setText(getIntent().getStringExtra("sg"));
//        tvKfpdYears.setText(getIntent().getStringExtra("age"));
        tvKfpdWeight.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 3);
                        tvKfpdWeight.setText(s);
                        tvKfpdWeight.setSelection(s.length());
                    }
                }
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    tvKfpdWeight.setText(s);
                    tvKfpdWeight.setSelection(2);
                }

                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        tvKfpdWeight.setText(s.subSequence(0, 1));
                        tvKfpdWeight.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }
        });
    }

    public void doClick(View v) {
        switch (v.getId()) {
            case R.id.bt_kfpd_star://开始填表
                try {
//                    if (TextUtils.isEmpty(tvKfpdYears.getText().toString())) {
//                        CommonUtil.showToask(KangFuPDActivity.this, "请输入您的年纪！");
//                        return;
//                    }
//                    if (tvKfpdYears.getText().toString().startsWith("0")) {
//                        CommonUtil.showToask(KangFuPDActivity.this, "请输入正确的年纪！");
//                        return;
//                    }
//                    //年龄在18-80之间，身高在130-220之间，体重在35-200kg之间
//                    if (Integer.parseInt(tvKfpdYears.getText().toString()) < 18 || Integer.parseInt(tvKfpdYears.getText().toString()) > 80) {
//                        CommonUtil.showToask(KangFuPDActivity.this, "请输入正确的年纪！");
//                        return;
//                    }
                    if (TextUtils.isEmpty(tvKfpdSg.getText().toString())) {
                        CommonUtil.showToask(KangFuPDActivity.this, "请输入您的身高！");
                        return;
                    }
                    if (tvKfpdSg.getText().toString().startsWith("0")) {
                        CommonUtil.showToask(KangFuPDActivity.this, "请输入正确的的身高！");
                        return;
                    }
                    //年龄在18-80之间，身高在130-220之间，体重在35-200kg之间
                    if (Integer.parseInt(tvKfpdSg.getText().toString()) < 130 || Integer.parseInt(tvKfpdSg.getText().toString()) > 220) {
                        CommonUtil.showToask(KangFuPDActivity.this, "请输入正确的身高！");
                        return;
                    }
                    if (TextUtils.isEmpty(tvKfpdWeight.getText().toString())) {
                        CommonUtil.showToask(KangFuPDActivity.this, "请输入您的体重！");
                        return;
                    }
                    if (tvKfpdWeight.getText().toString().endsWith(".")) {
                        CommonUtil.showToask(KangFuPDActivity.this, "请输入正确的的体重！");
                        return;
                    }
                    if (tvKfpdWeight.getText().toString().startsWith("0")) {
                        CommonUtil.showToask(KangFuPDActivity.this, "请输入正确的的体重！");
                        return;
                    }
                    if (tvKfpdWeight.getText().toString().endsWith(".")) {
                        CommonUtil.showToask(KangFuPDActivity.this, "请输入正确的的体重！");
                        return;
                    }
                    //年龄在18-80之间，身高在130-220之间，体重在35-200kg之间
                    if (Double.parseDouble(tvKfpdWeight.getText().toString()) < 35 || Double.parseDouble(tvKfpdWeight.getText().toString()) > 200) {
                        CommonUtil.showToask(KangFuPDActivity.this, "请输入正确的体重！");
                        return;
                    }
                } catch (Exception o) {

                }
                if (TextUtils.isEmpty(getIntent().getStringExtra("new"))) {
                    Intent in = new Intent(KangFuPDActivity.this, ChoosePainPlaceActivity.class);
                    in.putExtra("id", getIntent().getStringExtra("id"));
                    startActivity(in);
                } else {//来自新建
                    getCreateData();
                }
                break;


        }
    }

    /**
     * 新建接口
     */
    public void getCreateData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.CreateKFPD, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(KangFuPDActivity.this, "uid"));
        mRequest.add("age", tvKfpdYears.getText().toString());
        mRequest.add("weight", tvKfpdWeight.getText().toString());
        mRequest.add("stature", tvKfpdSg.getText().toString());
        // 添加到请求队列
        CallServer.getRequestInstance().add(KangFuPDActivity.this, 0, mRequest,
                new CustomHttpListener(KangFuPDActivity.this, true, CreateKFPDM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        CreateKFPDM model = ((CreateKFPDM) data);
                        Params.isDati = "0";
                        Intent in = new Intent(KangFuPDActivity.this, ChoosePainPlaceActivity.class);
                        in.putExtra("id", model.getObject());
                        startActivity(in);
                        Params.CreateKFPD = 1;
                        finish();
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        if (!"100".equals(obj.getString("msgcode"))) {
                            CommonUtil.showToask(KangFuPDActivity.this, obj.getString("msg"));
                        }
                    }
                }, true, true);
    }

    private void getPerData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.PerMess, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(KangFuPDActivity.this, "uid"));
        CallServer.getRequestInstance().add(KangFuPDActivity.this, 0, mRequest,
                new CustomHttpListener(KangFuPDActivity.this, true, PerMessM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        PerMessM model = ((PerMessM) data);
                        PreferencesUtils.putString(KangFuPDActivity.this, "age", model.getObject().getAge() + "");
                        tvKfpdYears.setText(model.getObject().getAge() + "");
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                    }
                }, true, true);
    }


}
